<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="添加广告账户" :visible.sync="dialogVisible" @close="resetForm()" width="600px">
        <el-input placeholder="请输入账户ID、账户名称进行搜索" clearable prefix-icon="el-icon-search" v-model="querytext">
        </el-input>
        <div v-loading="loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0)">
            <el-table :data="filteredData(tableData,querytext)" class="com_table" height="400px" :row-key="(row)=>{ return row.advertiser_id}" style="margin-top:20px" ref="planRef" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" :reserve-selection="true">
                </el-table-column>
                <el-table-column prop="advertiser_id" label="账户ID" width="180">
                </el-table-column>
                <el-table-column prop="name" label="账户名称">
                </el-table-column>
            </el-table>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm()" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm()" class="el-button-width">添 加</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    props:["addForm"],
    name: "",
    data() {
        return {
            types:null,
            loading: false,
            dialogVisible: false,
            hasCheck: [],
            querytext: "",
            hasPlan:[],
            tableData: [],
        };
    },
    created() {
        
    },
   
    methods: {
        getData(type){
            this.types = type
            if(type==1){
                this.hasPlan = JSON.parse(JSON.stringify(this.addForm.one.advertiser_id))
            }else if(type==2){
                this.hasPlan = JSON.parse(JSON.stringify(this.addForm.two.advertiser_id))
            }
            this.dialogVisible = true
            this.getDataList()
        },
        //获取广告组
        async getDataList() {
            this.loading = true;
            var { data: res } = await this.$http.post(
                "videotest/Probject/advertiser_list"
            );
            if (res.code !== 200) {
                this.loading = false;
                this.$message.error(res.msg);
                return;
            } else {
                this.loading = false;
                this.tableData = res.data;
                this.hasPlan.forEach((row) => {
                    this.$refs.planRef.toggleRowSelection(row, true);
                });
            }
        },

        // 添加
        submitForm() {
            if(this.types==1){
                this.addForm.one.advertiser_id = this.hasCheck
            }else if(this.types==2){
                this.addForm.two.advertiser_id = this.hasCheck
            }
            this.$emit("setSessionStorage")
            this.resetForm()
        },

        // 关闭重置表单
        resetForm() {
            this.querytext = "";
            this.tableData = []
            this.dialogVisible = false;
        },

        // 筛选
        filteredData(list, query) {
            var list = list,
                query = query;

            if (!query) {
                return list;
            }
            query = query.trim();

            list = list.filter(function (item) {
                if(!item.name){
                    return
                }
                if (
                    item.name.indexOf(query) !== -1 ||
                    item.advertiser_id == query
                ) {
                    return item;
                }
            });
            return list;
        },

        // 表格选中
        handleSelectionChange(v) {
            this.hasCheck = v;
        },
        
    },
    components: {},
};
</script>


<style scoped>
</style>
